package admin.controllers;

import helpers.CookieHelper;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import debugs.ApplicationMappingDebug;

import entities.ApplicationMapping;

import admin.models.ApplicationMappingModel;


public class ApplicationMappingDeleteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setContentType("text/html");
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String params = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (params == null || params.isEmpty()) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		String[] pr = params.split("/");
		
		if (pr.length < 2) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		String domain = pr[0];
		String id = pr[1];
		
		try {
			ApplicationMappingModel appMappingModel = ApplicationMappingModel.getInstance();
			ApplicationMapping appMapping = appMappingModel.get(domain);
			appMappingModel.delete(appMapping);
			CookieHelper.setCookie(response, "success", "Delete success appmapping " + id);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		response.sendRedirect("/admin/appmapping/list/" + id);
	}
}
